package com.cssw.fyzb.tps.api.adapay;

import com.cssw.fyzb.tps.config.adapay.AdapayConfig;
import com.cssw.fyzb.tps.service.pay.AliPayService;
import com.huifu.adapay.model.Drawcash;
import com.huifu.adapay.model.SettleAccount;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/06 22:29
 */
@Function(description = "发起转账", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "orderNo", type = Type.String, description = "订单号", required = true),
        @Parameter(name = "transAmt", type = Type.String, description = "转账金额，必须大于0，人民币为元，保留两位小数点，如0.10、100.05等", required = true),
})
@Component
public class CreateTransferAdapay implements Action {

    private static final Logger logger = LoggerFactory.getLogger(CreateTransferAdapay.class);

    @Autowired
    private AdapayConfig adapayConfig;

    @Override
    public Response execute(Request request) throws Exception {
        String userId = request.getString("userId");
        String orderNo = request.getString("orderNo");
        String transAmt = request.getString("transAmt");

        try {
            Map<String, Object> transferParams = new  HashMap<String, Object>(5);
            transferParams.put("order_no", orderNo);
            transferParams.put("trans_amt", transAmt);
            transferParams.put("app_id", adapayConfig.getAppId());
            transferParams.put("in_member_id", "0");
            transferParams.put("out_member_id", userId);
            Map<String, Object> cash = SettleAccount.transfer(transferParams);

            return Response.success().put("data", cash);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("转账发起失败");
        }
    }
}
